package com.mo.lib.utils.data

import java.util.*

/**
 * @ author：mo
 * @ data：2022/4/22:9:46
 * @ 功能：随机数
 */

/** boolean 随机数*/
fun getRandomBoolean() = Random().nextBoolean()

/** 0 到 max 随机数 (包含 max)*/
fun getRandomInt(max: Int) = Random().nextInt(max + 1)


/**  min 到 max 的随机数 (包含 max)*/
fun getRandomInt(min: Int, max: Int): Int {
    val diff = max - min
    return if (diff == 0) {
        min
    } else min + Random().nextInt(diff + 1)
}

/** 随机小写字母字符串*/
fun getRandomLetterLowerCase(length: Int) = getRandomString("abcdefghijklmnopqrstuvwxyz", length)

/** 随机大写字母字符串*/
fun getRandomLetterUpperCase(length: Int) = getRandomString("ABCDEFGHIJKLMNOPQRSTUVWXYZ", length)

/** 随机大小写字母字符串*/
fun getRandomLetter(length: Int) = getRandomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length)

/**随机字母 + 大小写字母字符串 */
fun getRandomNumberAndLetter(length: Int) = getRandomString("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length)

/** 在指定的字符数组所限定的字符中, 获取指定长度的随机字符串            source 字符源资源     length 字符串长度*/
fun getRandomString(source: String, length: Int) = getRandomString(source.toCharArray(), length)

/** 在指定的字符数组所限定的字符中, 获取指定长度的随机字符串            source 字符源资源     length 字符串长度  */
fun getRandomString(source: CharArray, length: Int): String {
    val str = StringBuilder(length)
    val random = Random()
    for (i in 0 until length) {
        str.append(source[random.nextInt(source.size)])
    }
    return str.toString()
}